package controllers.config;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import datasource.ConnectionSQLite3;
import model.ChannelCondition;
import play.mvc.Controller;
/**
 * 配置
 *
 *
 * @version 1.0
 * @since JDK1.7
 * @author yutao
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2017年5月15日下午4:37:50
 */
public class ConfigAction extends Controller {

	private static Logger logger = LogManager.getLogger(ConfigAction.class.getName());
	
	private static Connection connection = ConnectionSQLite3.datasource.getConnectionSQL("autodeploy");
	/**
	 * 获取配置信息
	 * 
	 * @author yutao
	 * @date 2017年5月15日下午4:50:39
	 */
	public static void query(ChannelCondition condition){
		String sql = "select * from codeshell where project_name = '" + condition.getProjectName() + "';";
		logger.debug(sql);
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		try {
//			connection.setAutoCommit(false);
			
			PreparedStatement ps = connection.prepareStatement(sql);
			
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				//command shell 命令
				String command = rs.getString("code_shell");
				
				result.put("command", command);
			}
			rs.close();
			ps.close();
			result.put("project_name", condition.getProjectName());
		} catch (SQLException e) {
			logger.error("------SQLException-----" + e.getStackTrace());
		}
		
		render(result);
	}
	
	/**
	 * 保存脚本
	 * @param condition
	 * @author yutao
	 * @date 2017年5月19日下午2:22:02
	 */
	public static void saveCodeShell(ChannelCondition condition){
		
		String codeShell = condition.getCodeShell();
		
		String insertSql = "insert into codeshell(code_shell, project_name) values(?,?)";
		String updateSql = "update codeshell set code_shell = ? where project_name = ?;";
		
		boolean exists = isExists(condition);
		boolean data = false;
		PreparedStatement ps = null;
		try {
			if(exists){
				ps = connection.prepareStatement(updateSql);
			}else{
				ps = connection.prepareStatement(insertSql);
			}
			ps.setString(1, condition.getCodeShell());
			ps.setString(2, condition.getProjectName());
			
			int i = ps.executeUpdate();
			
			if(i > 0){
				data = true;
			}
			logger.debug("---保存脚本执行的sql语句-----"+codeShell);
			connection.commit();
		} catch (SQLException e) {
			logger.error("-----执行sql异常----" + e.getStackTrace());
		}finally {
			if(ps != null){
				try {
					ps.close();
				} catch (SQLException e) {
					logger.error("----关闭sql----" + e.getStackTrace());
				}
			}
		}
		
		renderJSON(data);
	}

	/**
	 * 判断数据库中这条记录是否早已存在
	 * true 存在，false不存在
	 * @param condition
	 * @author yutao
	 * @date 2017年5月22日下午6:00:25
	 */
	private static boolean isExists(ChannelCondition condition) {
		String selectSql = "select count(*) count from codeshell where project_name = ?;";
		PreparedStatement ps = null;
		int count = 0;
		try {
			ps = connection.prepareStatement(selectSql);
			ps.setString(1, condition.getProjectName());
			
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				 count = rs.getInt("count");
			}
			rs.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(ps != null){
				try {
					ps.close();
				} catch (SQLException e) {
					logger.error("----关闭sql----" + e.getStackTrace());
				}
			}
		}
		
		if(count == 0 ){
			return false;
		}
		return true;
	}
	
	public static Connection getConnection(){
		return connection;
	}
}
